﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Visualizer3D
{
    public partial class ToolStripRadioButton : ToolStripButton
    {
        public ToolStripRadioButton()
        {
            InitializeComponent();
        }
        
        private void Initialize()
        {
            CheckOnClick = true;
        }

        protected override void OnCheckedChanged(EventArgs e)
        {
            base.OnCheckedChanged(e);

            // If this item is no longer in the checked state, do nothing.
            if (!Checked)
                return;

            if (null == Parent)
                return;
            
            // Clear the checked state for all siblings. 
            foreach (ToolStripItem item in Parent.Items)
            {
                ToolStripRadioButton radioItem =
                    item as ToolStripRadioButton;
                if (radioItem != null && 
                    radioItem != this && 
                    groupIndex != -1 && 
                    radioItem.groupIndex == groupIndex && 
                    radioItem.Checked)
                {
                    radioItem.Checked = false;

                    // Only one item can be selected at a time, 
                    // so there is no need to continue.
                    return;
                }
            }
        }

        protected override void OnClick(EventArgs e)
        {
            // If the item is already in the checked state, do not call 
            // the base method, which would toggle the value. 
            if (Checked)
                return;

            base.OnClick(e);
        }

        private int groupIndex = -1;

        [Description("Create group from separate tool strip buttons with the same radio index."), Category("Appearance")]
        public int GroupIndex
        {
            get
            {
                return groupIndex;
            }
            set
            {
                groupIndex = value;
            }
        }
    }
}
